﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM.Business
{
   public class Staff
    {
       private static CRM.DataLayer.CRMDateEntities db = new DataLayer.CRMDateEntities();
       /// <summary>
       /// 返回员工信息列表(殷路辉)
       /// </summary>
       /// <returns></returns>
       public static IList<CRM.DataLayer.Staff> PageListStaffs(out int totleCount, int pageNum, int pageSize)
       {
           try
           {
               using (db)
               {
                   var result = from staff in db.Staff
                                orderby staff.staffID descending
                                select staff;
                   //获取总记录数目
                   totleCount = result.ToArray().Length;
                   //获取请求页的最大索引
                   int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                   if (max < (pageNum - 1) * pageSize)
                   {
                       //请求页的最大索引比它上一页的最大索引还小：矛盾
                       return null;
                   }
                   else if (max < pageSize * pageNum)
                   {
                       //请求页是左后一页
                       return result.Skip((pageNum - 1) * pageSize).ToList();
                   }
                   else
                   {
                       //请求页正常返回
                       return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                   }
               }
           }
           catch (Exception)
           {
               totleCount = 0;
               return null;
           }
       }
       /// <summary>
       /// 根据员工id号获得员工信息(殷路辉)
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public static CRM.DataLayer.Staff GetMemberById(int id)
       {
           try
           {
               var result = db.Staff.SingleOrDefault(p => p.staffID == id);
               if (result != null)
                   return result;
               else
                   return null;
           }
           catch (Exception)
           {

               return null;
           }
       }
    }
}
